import Vue from 'vue'
import Router from 'vue-router'
import About from '../components/About.vue'
import user from '../components/user.vue'
// 通过Vue.use安装这个插件
Vue.use(Router)

const Home = () => import('../components/home.vue')
const HomeNews = () => import('../components/homeNews.vue')

const Profile = () => import('../components/Profile.vue')
const router = new Router({
  //配置路由和组件之间的应用关系
  routes: [
    {
      path: '/',
      //重定向
      redirect: '/home',
      meta: { title: '首页' },
    },
    {
      path: '/home',
      component: Home,
      meta: { title: '首页' },
      children: [
        {
          path: '',
          redirect: 'news',
        },
        {
          // 不需要前面加/
          path: 'news',
          component: HomeNews
        }
      ]
    },
    {
      path: '/about',
      component: About,
      meta: { title: '关于' },
    },
    {
      path: '/user/:userId',
      component: user,
      meta: { title: '用户' },
    },
    {
      path: '/profile',
      component: Profile,
      meta: { title: '我的' },
    }
  ],
  mode: 'history'
})

export default router


router.beforeEach((to, from, next) => {
  //从from跳转到to
  document.title = to.matched[0].meta.title
  console.log(to);
  next()
})